package cn.power.win.win_power.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.power.win.win_power.R;
import cn.power.win.win_power.utils.RxBus;
import cn.power.win.win_power.utils.SettingEvent2;
import me.jessyan.autosize.internal.CustomAdapt;
import rx.Subscription;
import rx.functions.Action1;

import static cn.power.win.win_power.fragment.ActualTimeFragment.Kvdatacs;
import static cn.power.win.win_power.fragment.ActualTimeFragment.System_parameter;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class SettingFragment extends Fragment implements View.OnClickListener, CustomAdapt {

    @BindView(R.id.ll_short_voltage)
    LinearLayout llShortVoltage;
    @BindView(R.id.ll_out_excessive_voltage)
    LinearLayout llOutExcessiveVoltage;
    @BindView(R.id.ll_daybreak_voltage)
    LinearLayout llDaybreakVoltage;
    @BindView(R.id.ll_day_dark_voltage)
    LinearLayout llDayDarkVoltage;
    @BindView(R.id.ll_short_voltage_recover)
    LinearLayout llShortVoltageRecover;

    private EditText mEtFanStartVoltage;              // 风机起始电压点
    private EditText mEtFanUpLoadingCurrent;          // 风机卸载电流
    private EditText mEtFanUpLoadingVoltage;          // 风机卸载电压
    private EditText mEtFanStopSpeed;                 // 风机停止转速
    private LinearLayout mLlSave;                     // 保存参数
    private LinearLayout mLlRestore;                  // 恢复出厂参数
    private float mFanStartVoltage;
    private float mFanUpLoadingCurrent;
    private float mFanUpLoadingVoltage;
    private RadioGroup mRgLayout;                     // 单选框
    private RadioButton mRbRun;                       // 运行
    private RadioButton mRbBrake;                     // 刹车
    private Button mLoad1;                            // load1 按钮
    private Button mLoad2;                            // Load2 按钮
    private LinearLayout mLlLoad1;                    //
    private LinearLayout mLlLoad2;
    private Spinner mSpinner;                         //  下拉框1
    private Spinner mSpinner2;                        //  下拉框2
    private ArrayAdapter<String> mSpinnerAdapter;
    private ArrayAdapter<String> mSpinnerAdapter2;
    private String mItem;

    byte cs_out[] = new byte[2];
    private EditText mEtHalfTimeStartTime;
    private EditText mEtTimeOffTime;
    private EditText mEtTimeOffTime2;
    private EditText mEtHalfTimeStartTime2;
    private int mTimeOffTime2;
    private int mHalfTimeStartTime2;
    private int mHalfTimeStartTime1;
    private int mTimeOffTime1;

    private String mItem2;
    private EditText mEtDaoNaZhi;
    private LinearLayout mLlDaoNaZhi;
    private EditText mEtShortVoltage;
    private float mShortVoltage;
    private EditText mEtOutExcessiveVoltage;
    private EditText mEtDayBreakVoltage;
    private EditText mEtDayDarkVoltage;
    private EditText mEtShortVoltageRecover;
    private View mVDaoNaZhi;
    private LinearLayout mLlFanStartVoltage;
    private LinearLayout mLlFanStopSpeed;
    // private LinearLayout mLlLoadSetting;
    private LinearLayout mLlLoad;
    private Subscription mRxSbscription;
    private View mView6;
    private View mView7;
    private View mView8;
    private View mView9;
    private View mView10;
    private Unbinder mUnbinder;
    private TextView mTvHandSetting;
    private LinearLayout mLlHandSetting;
    private int mDaoNa2;
    private LinearLayout mLlFanUploadingCurrent;
    private View mView2;
    private View mView3;
    private View mView5;
    private String mModel;


    public SettingFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initView(view);
        initData();
        initListener();
        /*
         * 储存八种方式
         * */
        cs_out[0] = ActualTimeFragment.System_parameter[23];
        cs_out[1] = ActualTimeFragment.System_parameter[26];

        return view;
    }

    //初始化界面
    private void initView(final View view) {
        mView2 = view.findViewById(R.id.view2);
        mView3 = view.findViewById(R.id.view3);
        mView5 = view.findViewById(R.id.view5);
        mView6 = view.findViewById(R.id.view6);
        mView7 = view.findViewById(R.id.view7);
        mView8 = view.findViewById(R.id.view8);
        mView9 = view.findViewById(R.id.view9);
        mView10 = view.findViewById(R.id.view10);

        mLlFanUploadingCurrent = (LinearLayout) view.findViewById(R.id.ll_fan_uploading_current);
        //欠压恢复点
        mEtShortVoltageRecover = (EditText) view.findViewById(R.id.et_short_voltage_recover);
        mEtShortVoltageRecover.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        //天亮检测点
        mEtDayBreakVoltage = (EditText) view.findViewById(R.id.et_daybreak_voltage);
        mEtDayBreakVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        //天黑监测点
        mEtDayDarkVoltage = (EditText) view.findViewById(R.id.et_day_dark_voltage);
        mEtDayDarkVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        //输出过压点
        mEtOutExcessiveVoltage = (EditText) view.findViewById(R.id.et_out_excessive_voltage);
        mEtOutExcessiveVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        //欠压点
        mEtShortVoltage = (EditText) view.findViewById(R.id.et_short_voltage);
        mEtShortVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        // 导纳值
        mEtDaoNaZhi = (EditText) view.findViewById(R.id.et_daonazhi);
        mEtDaoNaZhi.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        mLlDaoNaZhi = (LinearLayout) view.findViewById(R.id.ll_daonazhi);
        mVDaoNaZhi = view.findViewById(R.id.view_daonazhi);

        // 初始化界面
        mLoad1 = (Button) view.findViewById(R.id.load1);
        mLoad2 = (Button) view.findViewById(R.id.load2);
        mLlLoad = (LinearLayout) view.findViewById(R.id.ll_load);

        mLlLoad1 = (LinearLayout) view.findViewById(R.id.ll_load1);
        mLlLoad2 = (LinearLayout) view.findViewById(R.id.ll_load2);
        //    mLlLoadSetting = (LinearLayout) view.findViewById(R.id.ll_load_setting);
        // 下来框

        //手动设置
        mTvHandSetting = (TextView) view.findViewById(R.id.tv_hand_setting);
        mLlHandSetting = (LinearLayout) view.findViewById(R.id.ll_hand_setting);
        // 下拉框
        mSpinner = (Spinner) view.findViewById(R.id.spinner);
        mSpinner2 = (Spinner) view.findViewById(R.id.spinner2);

        // 半功率开始时间
        mEtHalfTimeStartTime = (EditText) view.findViewById(R.id.et_half_time_start_time);
        mEtHalfTimeStartTime.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        // 时控关时间
        mEtTimeOffTime = (EditText) view.findViewById(R.id.et_Time_off_time);
        mEtTimeOffTime.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        //  Load2 半功率开始时间
        mEtHalfTimeStartTime2 = (EditText) view.findViewById(R.id.et_half_time_start_time2);
        mEtHalfTimeStartTime2.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        // Load2 时空关时间
        mEtTimeOffTime2 = (EditText) view.findViewById(R.id.et_Time_off_time2);
        mEtTimeOffTime2.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        // 风机起始电压点
        mEtFanStartVoltage = (EditText) view.findViewById(R.id.et_fan_start_voltage);
        mEtFanStartVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        // 风机卸载电流点
        mEtFanUpLoadingCurrent = (EditText) view.findViewById(R.id.et_fan_uploading_current);
        mEtFanUpLoadingCurrent.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        // 风机卸载电压点
        mEtFanUpLoadingVoltage = (EditText) view.findViewById(R.id.et_fan_uploading_voltage);
        mEtFanUpLoadingVoltage.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        // 风机停止转速
        mEtFanStopSpeed = (EditText) view.findViewById(R.id.et_fan_stop_speed);
        mEtFanStopSpeed.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        mLlSave = (LinearLayout) view.findViewById(R.id.ll_save);
        mLlRestore = (LinearLayout) view.findViewById(R.id.ll_restore);

        mLlFanStartVoltage = (LinearLayout) view.findViewById(R.id.ll_fan_start_voltage);
        mLlFanStopSpeed = (LinearLayout) view.findViewById(R.id.ll_fan_stop_speed);

        mRgLayout = (RadioGroup) view.findViewById(R.id.rg_layout);
        mRbRun = (RadioButton) view.findViewById(R.id.rb_run);
        mRbBrake = (RadioButton) view.findViewById(R.id.rb_brake);
        mLlSave.setOnClickListener(this);
        mLlRestore.setOnClickListener(this);

        mRgLayout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = (RadioButton) view.findViewById(checkedId);
                if (radbtn.getText().equals("运行")) {         // 控制器正常运行
                    System_parameter[9] &= ~0x04;

                } else {
                    System_parameter[9] |= 0x04;               // 使控制器刹车。。
                }
            }
        });

    }

    //初始化数据
    private void initData() {
        //使用数组作为数据源
        final String arr[] = new String[]{"光控开关", "光控开，时控关", "光控开，时控半功率，光控关", "光控开，时控半功率，时控关", "常亮", "常灭",
                "常半功率"};
        final String arr2[] = new String[]{"光控开关", "光控开，时控关", "光控开，时控半功率，光控关", "光控开，时控半功率，时控关", "常亮", "常灭",
                "常半功率"};
        //创建spinner适配器(load1)
        mSpinnerAdapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, arr);
        //设置下拉列表的风格
        mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //设置spinner的适配器
        mSpinner.setAdapter(mSpinnerAdapter);
        mSpinnerAdapter2 = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, arr2);
        //设置下拉列表的风格
        mSpinnerAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //设置spinner的适配器
        mSpinner2.setAdapter(mSpinnerAdapter2);


        /*
         * DATA32(系统设置位)：4位是垂直轴降压控制位，此位是高电平，表示是垂直轴降压控制器，
         * 1.先判断是否有风机转速；2.垂直轴降压控制位，此位为高电平时，显示风机停止转速
         *
         * DATA2：0位是转速控制位，高电平表示有转速控制。无转速控制时不显示转速和相应设定（风机转速）。
         *
         * */


        if ((System_parameter[29] & 0x01) != 0) {
            //Load1
            initSpinnerData();
            mLoad1.setBackgroundResource(R.drawable.bg4);
            mLoad2.setBackgroundResource(R.drawable.bg5);
            mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
            mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
            mLlLoad1.setVisibility(View.VISIBLE);
            mLlLoad2.setVisibility(View.GONE);
            getLoad1Data();
            Log.i("Load1:", "--------");
        } else {
            //Load1
            initSpinnerData();
            mLoad1.setBackgroundResource(R.drawable.bg1);
            mLoad2.setBackgroundResource(R.drawable.bg2);
            mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
            mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
            mLlLoad1.setVisibility(View.GONE);
            mLlLoad2.setVisibility(View.VISIBLE);
            getLoad2Data();
            Log.i("Load2:", "--------");
        }
        //Load1
        // initSpinnerData();


    }

    //
    @Override
    public void onStart() {
        super.onStart();
        // 接受从实时界面传来的数据进行实时更新显示
        mRxSbscription = RxBus.getInstance().toObserverable(SettingEvent2.class).subscribe(new Action1<SettingEvent2>() {
            @Override
            public void call(SettingEvent2 settingEvent) {


                if ("001".equals(settingEvent.getMODEL())) {
                    // ------------------------风光互补控制器------------------------------------
                    /*
                     * DATA2:4位是低压充电功能选择位，高电平表示有低压充电功能。
                     * 无低压充电功能时不显示“导纳”参数和风机起始电压。（0x10）
                     * */
                    if ((System_parameter[9] & 0x10) != 0x10) {
                        mLlDaoNaZhi.setVisibility(View.VISIBLE);
                        mVDaoNaZhi.setVisibility(View.VISIBLE);
                        mLlFanStartVoltage.setVisibility(View.VISIBLE);
                    }
                    if ((System_parameter[9] & 0x01) != 0) {
                        // 显示风机转速
                        mLlFanStopSpeed.setVisibility(View.VISIBLE);
                        mView5.setVisibility(View.VISIBLE);
                    } else {
                        // 隐藏风机转速
                        mLlFanStopSpeed.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);

                    }
                    //  初始化数据
                    if ((System_parameter[29] & 0x01) != 0x01) {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg4);
                        mLoad2.setBackgroundResource(R.drawable.bg5);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLlLoad1.setVisibility(View.VISIBLE);
                        mLlLoad2.setVisibility(View.GONE);
                        getLoad1Data();

                    } else {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg1);
                        mLoad2.setBackgroundResource(R.drawable.bg2);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.VISIBLE);
                        getLoad2Data();

                    }
                   /*
                   * 参数时定义 DATA 1
                   * 7位是硬件有无直流输出控制位 (判断有无负载)  高电平表示有直流输出，低表示无。
                   * （高电平负载是否显示）300W,600W两种型号是2路输出，其它功率是1路输出。
                   * 无直流输出时，不显示输出相关的数据和参数。包括 输出电流，输出状态，欠压点，输出过压点，光控点等.
                   * */
                    if ((System_parameter[8] & 0x80) != 0) {
                        //有直流输出的情况下
                        mLlLoad.setVisibility(View.VISIBLE);
                        // 显示欠压点
                        llShortVoltage.setVisibility(View.VISIBLE);
                        // 判断是否显示导纳值和风机起始电压点
                        if ((System_parameter[9] & 0x10) != 0x10) {
                            mLlDaoNaZhi.setVisibility(View.VISIBLE);
                            mVDaoNaZhi.setVisibility(View.VISIBLE);
                            mLlFanStartVoltage.setVisibility(View.VISIBLE);
                        }

                        // 显示过压点
                        llOutExcessiveVoltage.setVisibility(View.VISIBLE);
                        mView6.setVisibility(View.VISIBLE);
                        // 显示天亮电压点
                        llDaybreakVoltage.setVisibility(View.VISIBLE);
                        mView8.setVisibility(View.VISIBLE);
                        // 显示天黑电压点
                        llDayDarkVoltage.setVisibility(View.VISIBLE);
                        mView9.setVisibility(View.VISIBLE);
                        // 欠压恢复点
                        llShortVoltageRecover.setVisibility(View.VISIBLE);
                        mView10.setVisibility(View.VISIBLE);
                        if ((System_parameter[9] & 0x01) != 0) {
                            // 显示风机转速
                            mLlFanStopSpeed.setVisibility(View.VISIBLE);
                            mView5.setVisibility(View.VISIBLE);
                        } else {

                            mLlFanStopSpeed.setVisibility(View.GONE);
                            mView5.setVisibility(View.GONE);
                            // 隐藏风机转速
                        }

                    } else {
                        // 无直流输入的情况下
                        // 隐藏导纳值
                        mLlDaoNaZhi.setVisibility(View.GONE);
                        mVDaoNaZhi.setVisibility(View.GONE);
                        // 隐藏风机起始电压点
                        mLlFanStartVoltage.setVisibility(View.GONE);
                        llShortVoltage.setVisibility(View.GONE);
                        // 隐藏过压点
                        llOutExcessiveVoltage.setVisibility(View.GONE);
                        // 隐藏天黑电压点
                        llDaybreakVoltage.setVisibility(View.GONE);
                        // 隐藏天亮电压点
                        llDayDarkVoltage.setVisibility(View.GONE);
                        // 欠压恢复点
                        llShortVoltageRecover.setVisibility(View.GONE);
                        // 隐藏Load1和Load2设置界面
                        mLlLoad.setVisibility(View.GONE);
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.GONE);
                        mView2.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);
                        mView6.setVisibility(View.GONE);
                        mView7.setVisibility(View.GONE);
                        mView8.setVisibility(View.GONE);
                        mView9.setVisibility(View.GONE);
                        mView10.setVisibility(View.GONE);
                        // 显示手动设置
                        mTvHandSetting.setVisibility(View.VISIBLE);
                        mLlHandSetting.setVisibility(View.VISIBLE);

                    }
                    // 判断 手动设置的状态
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        //   运行状态
                        mRbRun.setChecked(true);
                    } else {
                        //   刹车状态
                        mRbBrake.setChecked(true);
                    }
                    //  设置显示的导纳值
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }

                    // 风机停止转速
                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    // 卸载电压点
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    // 卸载电流点
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    // 风机开始电压点
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    // 天黑电压点
                    mEtDayBreakVoltage.setText(settingEvent.getDayBreakVoltage());
                    // 天亮电压点
                    mEtDayDarkVoltage.setText(settingEvent.getDayDarkVoltage());
                    // 欠压点
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    // 过压点
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    // 欠压恢复点
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());
                    // 半功率起始时间 和 时空关时间
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");

                } else if ("002".equals(settingEvent.getMODEL())) {
                    //---------------------纯光伏控制器----------------------------------
                    if ((System_parameter[29] & 0x01) != 0x01) {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg4);
                        mLoad2.setBackgroundResource(R.drawable.bg5);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLlLoad1.setVisibility(View.VISIBLE);
                        mLlLoad2.setVisibility(View.GONE);
                        getLoad1Data();

                    } else {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg1);
                        mLoad2.setBackgroundResource(R.drawable.bg2);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.VISIBLE);
                        getLoad2Data();

                    }

                    if ((System_parameter[9] & 0x01) != 0) {
                        // 显示风机转速
                        mLlFanStopSpeed.setVisibility(View.VISIBLE);
                        mView5.setVisibility(View.VISIBLE);
                    } else {

                        mLlFanStopSpeed.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);
                        // 隐藏风机转速
                    }
                    // 风机状态判断
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        mRbRun.setChecked(true);
                    } else {
                        mRbBrake.setChecked(true);
                    }
                    // 导纳值
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }
                    // 隐藏风机卸载电流点
                    mLlFanUploadingCurrent.setVisibility(View.GONE);
                    mView2.setVisibility(View.GONE);
                    // 隐藏风机起始电压点
                    mLlFanStartVoltage.setVisibility(View.GONE);
                    mView3.setVisibility(View.GONE);
                    // 隐藏导纳值
                    mLlDaoNaZhi.setVisibility(View.GONE);
                    mVDaoNaZhi.setVisibility(View.GONE);
                    // 隐藏风机停止转速
                    mLlFanStopSpeed.setVisibility(View.GONE);
                    mView5.setVisibility(View.GONE);
                    // 隐藏手动设置功能
                    mTvHandSetting.setVisibility(View.GONE);
                    mLlHandSetting.setVisibility(View.GONE);
                    // 显示Load1和Load2的设置功能
                    mLlLoad.setVisibility(View.VISIBLE);
                    llShortVoltage.setVisibility(View.VISIBLE);
                    mView6.setVisibility(View.VISIBLE);
                    // 显示过压点
                    llOutExcessiveVoltage.setVisibility(View.VISIBLE);
                    // 显示天亮电压点
                    llDaybreakVoltage.setVisibility(View.VISIBLE);
                    mView8.setVisibility(View.VISIBLE);
                    // 显示天黑电压点
                    llDayDarkVoltage.setVisibility(View.VISIBLE);
                    mView9.setVisibility(View.VISIBLE);
                    // 欠压恢复点
                    llShortVoltageRecover.setVisibility(View.VISIBLE);
                    mView10.setVisibility(View.VISIBLE);
                    // 风机转速
                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    // 风机开始电压点
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    mEtDayBreakVoltage.setText(settingEvent.getDayBreakVoltage());
                    mEtDayDarkVoltage.setText(settingEvent.getDayDarkVoltage());
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());
                    // 半功率开始时间 和 时空关时间
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");
                } else if ("003".equals(settingEvent.getMODEL())) {
                    //-------------------------------10kw控制逆变一体机--------------------------------------
                    // 卸载电流点
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    // 卸载电压点
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    // 风机停止转速
                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    //风机开始电压点
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        mRbRun.setChecked(true);
                    } else {
                        mRbBrake.setChecked(true);
                    }
                    mLlLoad.setVisibility(View.GONE);
                    mLlLoad1.setVisibility(View.GONE);
                    mLlLoad2.setVisibility(View.GONE);

            /*        // 根据系统判断是否显示导纳值和风机起始电压点
                    if ((System_parameter[9] & 0x10) != 0) {
                        mLlDaoNaZhi.setVisibility(View.GONE);
                        mVDaoNaZhi.setVisibility(View.GONE);
                        mLlFanStartVoltage.setVisibility(View.VISIBLE);
                    }*/
                    mLlFanStartVoltage.setVisibility(View.VISIBLE);
                    mLlDaoNaZhi.setVisibility(View.GONE);
                    mVDaoNaZhi.setVisibility(View.GONE);
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }

                    mEtDayBreakVoltage.setText(settingEvent.getDayBreakVoltage());
                    mEtDayDarkVoltage.setText(settingEvent.getDayDarkVoltage());
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");

                } else if ("004".equals(settingEvent.getMODEL())) {
                    //   ---------------------- 风机控制器 ------------------------------
                    /*
                     * DATA2:4位是低压充电功能选择位，高电平表示有低压充电功能。
                     * 无低压充电功能时不显示“导纳”参数和风机起始电压。（0x10）
                     * */
                    if ((System_parameter[9] & 0x10) != 0x10) {
                        mLlDaoNaZhi.setVisibility(View.VISIBLE);
                        mVDaoNaZhi.setVisibility(View.VISIBLE);
                        mLlFanStartVoltage.setVisibility(View.VISIBLE);
                    }

                    if ((System_parameter[29] & 0x01) != 0x01) {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg4);
                        mLoad2.setBackgroundResource(R.drawable.bg5);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLlLoad1.setVisibility(View.VISIBLE);
                        mLlLoad2.setVisibility(View.GONE);
                        getLoad1Data();

                    } else {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg1);
                        mLoad2.setBackgroundResource(R.drawable.bg2);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.VISIBLE);
                        getLoad2Data();

                    }
                    if ((System_parameter[9] & 0x01) != 0) {
                        // 显示风机转速
                        mLlFanStopSpeed.setVisibility(View.VISIBLE);
                        mView5.setVisibility(View.VISIBLE);
                    } else {

                        mLlFanStopSpeed.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);
                        // 隐藏风机转速
                    }
                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    // 欠压点显示
                    mLlLoad.setVisibility(View.VISIBLE);
                    llShortVoltage.setVisibility(View.VISIBLE);
                    // 显示过压点
                    mView6.setVisibility(View.VISIBLE);
                    llOutExcessiveVoltage.setVisibility(View.VISIBLE);
                    //显示天黑电压点
                    llDaybreakVoltage.setVisibility(View.VISIBLE);
                    mView8.setVisibility(View.VISIBLE);
                    //显示天黑电压点
                    llDayDarkVoltage.setVisibility(View.VISIBLE);
                    mView9.setVisibility(View.VISIBLE);
                    //显示欠压恢复点
                    llShortVoltageRecover.setVisibility(View.VISIBLE);
                    mView10.setVisibility(View.VISIBLE);
                    // 设置风机卸载电压
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    //设置风机卸载电流
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    //风机开始电压点
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        mRbRun.setChecked(true);
                    } else {
                        mRbBrake.setChecked(true);
                    }
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }

                    mEtDayBreakVoltage.setText(settingEvent.getDayBreakVoltage());
                    mEtDayDarkVoltage.setText(settingEvent.getDayDarkVoltage());
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");

                } else if ("002".equals(settingEvent.getMODEL())) {
                    //---------------------光伏控制器----------------------------------
                    // 判断初始加载Load1和Load2的值
                    if ((System_parameter[29] & 0x01) != 0x01) {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg4);
                        mLoad2.setBackgroundResource(R.drawable.bg5);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLlLoad1.setVisibility(View.VISIBLE);
                        mLlLoad2.setVisibility(View.GONE);
                        getLoad1Data();

                    } else {
                        // 初始化下拉框数据
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg1);
                        mLoad2.setBackgroundResource(R.drawable.bg2);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.VISIBLE);
                        getLoad2Data();

                    }
                    //设置风机卸载电流
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    // 若有转速控制功能，一定会显示风机停止转速；
                    if ((System_parameter[9] & 0x01) != 0) {
                        // 显示风机停止转速
                        mLlFanStopSpeed.setVisibility(View.VISIBLE);
                        mView5.setVisibility(View.VISIBLE);
                    } else {
                        // 隐藏风机停止转速
                        mLlFanStopSpeed.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);

                    }

                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    //风机开始电压点
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    // 判断是运行状态还是刹车状态
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        mRbRun.setChecked(true);
                    } else {
                        mRbBrake.setChecked(true);
                    }

                    // 隐藏导纳值
                    mLlDaoNaZhi.setVisibility(View.GONE);
                    mVDaoNaZhi.setVisibility(View.GONE);
                    // 设置导纳值
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }
                    // 风机卸载电流
                    mLlFanUploadingCurrent.setVisibility(View.GONE);
                    mView2.setVisibility(View.GONE);
                    // 隐藏起始电压点
                    mLlFanStartVoltage.setVisibility(View.GONE);
                    mView3.setVisibility(View.GONE);

                    // 隐藏风机停止转速
                    mLlFanStopSpeed.setVisibility(View.GONE);
                    mView5.setVisibility(View.GONE);

                    // 隐藏手动设置
                    mTvHandSetting.setVisibility(View.GONE);
                    mLlHandSetting.setVisibility(View.GONE);

                    // 显示欠压点
                    mLlLoad.setVisibility(View.VISIBLE);
                    llShortVoltage.setVisibility(View.VISIBLE);
                    // 显示过压点
                    mView6.setVisibility(View.VISIBLE);
                    llOutExcessiveVoltage.setVisibility(View.VISIBLE);
                    // 隐藏天亮电压点
                    llDaybreakVoltage.setVisibility(View.GONE);
                    mView8.setVisibility(View.GONE);
                    // 隐藏天黑电压点
                    llDayDarkVoltage.setVisibility(View.GONE);
                    mView9.setVisibility(View.GONE);
                    // 显示欠压恢复点
                    llShortVoltageRecover.setVisibility(View.VISIBLE);
                    mView10.setVisibility(View.VISIBLE);
                    // 风机卸载电压点
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    // 天亮电压点
                    mEtDayBreakVoltage.setText(settingEvent.getDayBreakVoltage());
                    // 天黑电压点
                    mEtDayDarkVoltage.setText(settingEvent.getDayDarkVoltage());
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    // 过压点
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());
                     // Load1半功率开始时间
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    //  Load1时空关时间
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    // Load2半功率开始时间
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    // Load2时空关时间
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");


                } else if ("005".equals(settingEvent.getMODEL())) {
                    //--------------- 逆变器 --------------
                    //卸载电压点
                    mEtFanUpLoadingVoltage.setText(settingEvent.getFanUnloadingVoltage());
                    //设置风机卸载电流
                    mEtFanUpLoadingCurrent.setText(settingEvent.getFanUnloadingCurrent());
                    //隐藏起始电压点
                    mLlFanStartVoltage.setVisibility(View.GONE);
                    mView3.setVisibility(View.GONE);
                    mEtFanStartVoltage.setText(settingEvent.getFanBeginChargeVoltage());
                    // 隐藏导纳值

                    mLlDaoNaZhi.setVisibility(View.GONE);
                    mVDaoNaZhi.setVisibility(View.GONE);
                    // 设置导纳值
                    if ("255".equals(settingEvent.getDNZ() + "")) {
                        mEtDaoNaZhi.setText("MPPT");
                    } else {
                        mEtDaoNaZhi.setText(settingEvent.getDNZ() + "");
                    }

                    // 若有转速控制功能，一定会显示风机停止转速；
                    if ((System_parameter[9] & 0x01) != 0) {
                        // 显示风机停止转速
                        mLlFanStopSpeed.setVisibility(View.VISIBLE);
                        mView5.setVisibility(View.VISIBLE);
                    } else {
                        // 隐藏风机停止转速
                        mLlFanStopSpeed.setVisibility(View.GONE);
                        mView5.setVisibility(View.GONE);
                    }

                    // 隐藏风机停止转速
                    mEtFanStopSpeed.setText(settingEvent.getFanStopSpeed() + "");
                    // 隐藏手动设置
                    mTvHandSetting.setVisibility(View.GONE);
                    mLlHandSetting.setVisibility(View.GONE);
                    // 判断是运行状态还是刹车状态
                    boolean run_state = settingEvent.isRun_state();
                    if (run_state) {
                        mRbRun.setChecked(true);
                    } else {
                        mRbBrake.setChecked(true);
                    }
                    //欠压点
                    mEtShortVoltage.setText(settingEvent.getShortVoltage());
                    //输出过压点
                    mEtOutExcessiveVoltage.setText(settingEvent.getOutExcessiveVoltage());
                    //欠压恢复点
                    mEtShortVoltageRecover.setText(settingEvent.getShortVoltageRecover());

                    if ((System_parameter[29] & 0x01) != 0x01) {
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg4);
                        mLoad2.setBackgroundResource(R.drawable.bg5);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLlLoad1.setVisibility(View.VISIBLE);
                        mLlLoad2.setVisibility(View.GONE);
                        getLoad1Data();

                    } else {
                        // 初始化下拉框数据
                        initSpinnerData();
                        mLoad1.setBackgroundResource(R.drawable.bg1);
                        mLoad2.setBackgroundResource(R.drawable.bg2);
                        mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                        mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                        mLlLoad1.setVisibility(View.GONE);
                        mLlLoad2.setVisibility(View.VISIBLE);
                        getLoad2Data();

                    }
                    // Load1半功率开始时间
                    mEtHalfTimeStartTime.setText(settingEvent.getHalfTimeStartTime1() + "");
                    // Load1时空关时间
                    mEtTimeOffTime.setText(settingEvent.getTimeOffTime1() + "");
                    // Load2半功率开始时间
                    mEtHalfTimeStartTime2.setText(settingEvent.getHalfTimeStartTime2() + "");
                    // Load2时空关时间
                    mEtTimeOffTime2.setText(settingEvent.getTimeOffTime2() + "");


                }

            }
        });
    }

    //  初始化下拉框数据
    private void initSpinnerData() {
        if ((System_parameter[23] & 0x01) != 0) {
            Log.i("Load1", "0x01");
            mSpinner.setSelection(0, true);//设置默认 Light-control on and off
            mEtHalfTimeStartTime.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime.setEnabled(false);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        } else if ((System_parameter[23] & 0x02) != 0) {
            Log.i("Load1", "0x02");
            mSpinner.setSelection(1, true);//Light-control on ,time-control off
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime.setEnabled(false);
            //设置光控结束文本框可编辑
            mEtTimeOffTime.setEnabled(true);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
        } else if ((ActualTimeFragment.System_parameter[23] & 0x04) != 0) {
            Log.i("Load1", "0x04");
            mSpinner.setSelection(2, true);//Light-control on ,time-half power,light-control off
            // 设置光控开始时间文本框可编辑
            mEtHalfTimeStartTime.setEnabled(true);
            //设置光控结束文本框可编辑
            mEtTimeOffTime.setEnabled(false);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));

        } else if ((ActualTimeFragment.System_parameter[23] & 0x08) != 0) {
            Log.i("Load1", "0x08");
            mSpinner.setSelection(3, true);//Light-control on ,time-half power,time-control off
            // 设置光控开始时间文本框可编辑
            mEtHalfTimeStartTime.setEnabled(true);
            //设置光控结束文本框可编辑
            mEtTimeOffTime.setEnabled(true);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));

        } else if ((ActualTimeFragment.System_parameter[23] & 0x10) != 0) {
            Log.i("Load1", "0x10");
            mSpinner.setSelection(4, true);//Constant on
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime.setEnabled(false);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        } else if ((ActualTimeFragment.System_parameter[23] & 0x20) != 0) {
            Log.i("Load1", "0x20");
            mSpinner.setSelection(5, true);//Constant off
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime.setEnabled(false);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        } else if ((ActualTimeFragment.System_parameter[23] & 0x40) != 0) {
            Log.i("Load1", "0x40");
            mSpinner.setSelection(6, true);//Constant half-power
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime.setEnabled(false);
            mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        }

        //Load2---------mEtHalfTimeStartTime2-----mEtTimeOffTime2---------------------------------------------------------------
        if ((ActualTimeFragment.System_parameter[26] & 0x01) != 0) {
            mSpinner2.setSelection(0, true);//设置默认 Light-control on and off
            mEtHalfTimeStartTime2.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime2.setEnabled(false);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));

        } else if ((ActualTimeFragment.System_parameter[26] & 0x02) != 0) {

            mSpinner2.setSelection(1, true);//Light-control on ,time-control off
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime2.setEnabled(false);
            //设置光控结束文本框可编辑
            mEtTimeOffTime2.setEnabled(true);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
        } else if ((ActualTimeFragment.System_parameter[26] & 0x04) != 0) {
            mSpinner2.setSelection(2, true);//Light-control on ,time-half power,light-control off
            // 设置光控开始时间文本框可编辑
            mEtHalfTimeStartTime2.setEnabled(true);
            //设置光控结束文本框可编辑
            mEtTimeOffTime2.setEnabled(false);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));

        } else if ((ActualTimeFragment.System_parameter[26] & 0x08) != 0) {
            mSpinner2.setSelection(3, true);//Light-control on ,time-half power,time-control off
            // 设置光控开始时间文本框可编辑
            mEtHalfTimeStartTime2.setEnabled(true);
            //设置光控结束文本框可编辑
            mEtTimeOffTime2.setEnabled(true);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));

        } else if ((ActualTimeFragment.System_parameter[26] & 0x10) != 0) {
            mSpinner2.setSelection(4, true);//Constant on
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime2.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime2.setEnabled(false);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        } else if ((ActualTimeFragment.System_parameter[26] & 0x20) != 0) {
            mSpinner2.setSelection(5, true);//Constant off
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime2.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime2.setEnabled(false);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        } else if ((ActualTimeFragment.System_parameter[26] & 0x40) != 0) {
            mSpinner2.setSelection(6, true);//Constant half-power
            // 设置光控开始时间文本框不可编辑
            mEtHalfTimeStartTime2.setEnabled(false);
            //设置光控结束文本框不可编辑
            mEtTimeOffTime2.setEnabled(false);
            mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
            mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
        }
    }

    //下拉框1的数据展示
    private void getLoad1Data() {
        //添加事件Spinner事件监听
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // view2.setText("你选中的："+mSpinnerAdapter.getItem(position));
                mItem = mSpinnerAdapter.getItem(position);
                //  Toast.makeText(getContext(),mItem,Toast.LENGTH_SHORT).show();
                TextView tv = (TextView) view;
                tv.setTextSize(13.0f);
                tv.setTextColor(getResources().getColor(R.color.colorBlack));


                //光控开关Light-control on and off
                if ("光控开关".equals(mItem)) {
                    // 设置光控开始时间文本框不可编辑
                    cs_out[0] = (byte) 0x01;

                    mEtHalfTimeStartTime.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime.setEnabled(false);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));

                } else if ("光控开，时控关".equals(mItem)) { //光控开 时空关"Light-control on ,time-control off"
                    cs_out[0] = (byte) 0x02;

                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime.setEnabled(false);
                    //设置光控结束文本框可编辑
                    mEtTimeOffTime.setEnabled(true);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));

                } else if ("光控开，时控半功率，光控关".equals(mItem)) {//光控开，时空半功率，光控关 "Light-control on ,time-half power,light-control off"

                    cs_out[0] = (byte) 0x04;

                    // 设置光控开始时间文本框可编辑
                    mEtHalfTimeStartTime.setEnabled(true);
                    //设置光控结束文本框可编辑
                    mEtTimeOffTime.setEnabled(false);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("光控开，时控半功率，时控关".equals(mItem)) {//"Light-control on ,time-half power,time-control off"

                    cs_out[0] = (byte) 0x08;
                    // 设置光控开始时间文本框可编辑
                    mEtHalfTimeStartTime.setEnabled(true);
                    //设置光控结束文本框可编辑
                    mEtTimeOffTime.setEnabled(true);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                } else if ("常亮".equals(mItem)) {//"Constant on" 常亮

                    cs_out[0] = (byte) 0x10;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime.setEnabled(false);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("常灭".equals(mItem)) {//"Constant off" 常灭

                    cs_out[0] = (byte) 0x20;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime.setEnabled(false);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("常半功率".equals(mItem)) {//"Constant half-power" 常半功率

                    cs_out[0] = (byte) 0x40;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime.setEnabled(false);
                    mEtHalfTimeStartTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    //下拉框2的数据展示
    private void getLoad2Data() {

        mSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mItem2 = mSpinnerAdapter.getItem(position);

                //调整下拉框里的字体大小
                TextView tv2 = (TextView) view;
                tv2.setTextSize(13.0f);
                tv2.setTextColor(getResources().getColor(R.color.colorBlack));

                //光控开关Light-control on and off
                if ("光控开关".equals(mItem2)) {
                    // 设置光控开始时间文本框不可编辑
                    Log.i("light control", mItem2 + "");
                    cs_out[1] = (byte) 0x01;
                    mEtHalfTimeStartTime2.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime2.setEnabled(false);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("光控开，时控关".equals(mItem2)) { //光控开 时空关"Light-control on ,time-control off"
                    cs_out[1] = (byte) 0x02;

                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime2.setEnabled(false);
                    //设置光控结束文本框可编辑
                    mEtTimeOffTime2.setEnabled(true);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));


                } else if ("光控开，时控半功率，光控关".equals(mItem2)) {//光控开，时空半功率，光控关 "Light-control on ,time-half power,light-control off"
                    cs_out[1] = (byte) 0x04;
                    Log.e("TEST", "光控开，时空半功率，光控关");
                    // 设置光控开始时间文本框可编辑
                    mEtHalfTimeStartTime2.setEnabled(true);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime2.setEnabled(false);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));


                } else if ("光控开，时控半功率，时控关".equals(mItem2)) {//"Light-control on ,time-half power,time-control off"
                    cs_out[1] = (byte) 0x08;
                    // 设置光控开始时间文本框可编辑
                    mEtHalfTimeStartTime2.setEnabled(true);
                    //设置光控结束文本框可编辑
                    mEtTimeOffTime2.setEnabled(true);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button_white));
                } else if ("常亮".equals(mItem2)) {//"Constant on" 常亮
                    cs_out[1] = (byte) 0x10;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime2.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime2.setEnabled(false);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("常灭".equals(mItem2)) {  //"Constant off" 常灭

                    cs_out[1] = (byte) 0x20;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime2.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime2.setEnabled(false);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                } else if ("常半功率".equals(mItem2)) {  //"Constant half-power" 常半功率

                    cs_out[1] = (byte) 0x40;
                    // 设置光控开始时间文本框不可编辑
                    mEtHalfTimeStartTime2.setEnabled(false);
                    //设置光控结束文本框不可编辑
                    mEtTimeOffTime2.setEnabled(false);
                    mEtHalfTimeStartTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                    mEtTimeOffTime2.setBackground(getResources().getDrawable(R.drawable.shape_button2_gray));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initListener() {
        mLoad1.setOnClickListener(this);
        mLoad2.setOnClickListener(this);
        mLlSave.setOnClickListener(this);
        mLlRestore.setOnClickListener(this);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.load1:
                mLoad1.setBackgroundResource(R.drawable.bg4);
                mLoad2.setBackgroundResource(R.drawable.bg5);
                mLoad1.setTextColor(getResources().getColor(R.color.colorWhite));
                mLoad2.setTextColor(getResources().getColor(R.color.colorTheme));
                mLlLoad1.setVisibility(View.VISIBLE);
                mLlLoad2.setVisibility(View.GONE);
                getLoad1Data();
                break;

            case R.id.load2:
                mLoad1.setBackgroundResource(R.drawable.bg1);
                mLoad2.setBackgroundResource(R.drawable.bg2);
                mLoad1.setTextColor(getResources().getColor(R.color.colorTheme));
                mLoad2.setTextColor(getResources().getColor(R.color.colorWhite));
                mLlLoad1.setVisibility(View.GONE);
                mLlLoad2.setVisibility(View.VISIBLE);
                getLoad2Data();
                break;
            case R.id.ll_save:

                if (mEtFanStartVoltage.getText().toString().equals("")) {

                    Toast.makeText(getActivity(), "风机起始电压不能为空", Toast.LENGTH_SHORT).show();

                } else if (mEtFanUpLoadingCurrent.getText().toString().equals("")) {

                    Toast.makeText(getActivity(), "风机电流点不能为空", Toast.LENGTH_SHORT).show();
                    //卸载电流点(0~20A)
                } else if (mEtOutExcessiveVoltage.getText().toString().equals("")) {
                    Toast.makeText(getActivity(), "输出过压点不能为空", Toast.LENGTH_SHORT).show();
                } else if (mEtFanUpLoadingVoltage.getText().toString().equals("")) {
                    Toast.makeText(getActivity(), "风机卸载电压点不能为空", Toast.LENGTH_SHORT).show();
                    //卸载电压点（26~30V）

                } else if (mEtFanStopSpeed.getText().toString().equals("")) {
                    Toast.makeText(getActivity(), "风机停止速度不能为空", Toast.LENGTH_SHORT).show();
                } else if (mEtDaoNaZhi.getText().toString().equals("")) {
                    Toast.makeText(getActivity(), "导纳值不能为空", Toast.LENGTH_SHORT).show();
                } else if ((Float.parseFloat(mEtDayBreakVoltage.getText().toString()) / (float) Kvdatacs) > 255) {
                    //判断天亮值是否合理 天亮检测点（1~5v）
                    Toast.makeText(getActivity(), "天亮点必须小于" + (255 * (float) Kvdatacs) + "V", Toast.LENGTH_SHORT).show();
                } else if ((Float.parseFloat(mEtDayDarkVoltage.getText().toString()) / (float) Kvdatacs) > 255)            //----判断天黑点是否合理
                {
                    //判断天黑点是否合理 天黑检测点
                    Toast.makeText(getActivity(), getResources().getString(R.string.LightOffVoltageMustBeLessThan) + (255 * (float) Kvdatacs) + "V",                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else if ((Float.parseFloat(mEtShortVoltage.getText().toString()) / (float) Kvdatacs) >= (Float.parseFloat(mEtOutExcessiveVoltage.getText().toString()) / (float) Kvdatacs))            //----判断欠压点应先与过压点
                {

                    Toast.makeText(getActivity(), getResources().getString(R.string.UndervoltageMustBeLessThanOverVoltage),                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                    //  是欠压点（20~25）
                } else if ((Float.parseFloat(mEtShortVoltage.getText().toString()) / (float) Kvdatacs) >= (Float.parseFloat(mEtShortVoltageRecover.getText().toString()) / (float) Kvdatacs))            //----判断欠压点应小于欠压恢复点
                {

                    Toast.makeText(getActivity(), getResources().getString(R.string.UndervoltageMustBeLessThanUndervoltageRecoverye),                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else if (((cs_out[0] & 0x0c) != 0) && ((Integer.parseInt(mEtHalfTimeStartTime.getText().toString())) > 255))            //----判断卸荷电流是否合理
                {
                    Toast.makeText(getActivity(), "Load1HalfPowerOnCanNotBeMoreThan255",                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else if (((cs_out[0] & 0x0a) != 0) && ((Integer.parseInt(mEtTimeOffTime.getText().toString())) > 255))            //----判断卸荷电流是否合理
                {
                    Toast.makeText(getActivity(), "Load1TimeControlOffCanNotBeMoreThan255",                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else if (((cs_out[1] & 0x0c) != 0) && ((Integer.parseInt(mEtHalfTimeStartTime2.getText().toString())) > 255))            //----判断卸荷电流是否合理
                {
                    Toast.makeText(getActivity(), "Load2HalfPowerOnCanNotBeMoreThan255",                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else if (((cs_out[1] & 0x0a) != 0) && ((Integer.parseInt(mEtTimeOffTime2.getText().toString())) > 255))            //----判断卸荷电流是否合理
                {
                    Toast.makeText(getActivity(), "Load2TimeControlOffCanNotBeMoreThan255",                //小于可设置的最大值
                            Toast.LENGTH_SHORT).show();
                } else {
                    //------------------------------反解析参数--------------------------------
                    //反解析风机起始电压点
                    mFanStartVoltage = Float.parseFloat(mEtFanStartVoltage.getText().toString());
                    System_parameter[22] = (byte) (mFanStartVoltage / (float) Kvdatacs);
                    //反解析风机卸荷电流点
                    mFanUpLoadingCurrent = Float.parseFloat(mEtFanUpLoadingCurrent.getText().toString());
                    System_parameter[11] = (byte) (mFanUpLoadingCurrent / (float) ActualTimeFragment.Kidatacs);
                    //反解析风机卸荷电压点
                    mFanUpLoadingVoltage = Float.parseFloat(mEtFanUpLoadingVoltage.getText().toString());
                    System_parameter[10] = (byte) (mFanUpLoadingVoltage / (float) Kvdatacs);

                    //反解析风机停止转速

                    int fanStopSpeed = (int) Double.parseDouble(mEtFanStopSpeed.getText().toString());//String的格式为x.00,那么肯定不能之间通过类型转换，将字符串转为int，可以先将字符串转为double，再将其强制转换转int。
                    System_parameter[19] = (byte) (fanStopSpeed / 256);//(fanStopSpeed & 0xff >> 8);
                    System_parameter[20] = (byte) (fanStopSpeed % 256);// (fanStopSpeed & 0xff);

                    // 反解析导纳值

                    if (mEtDaoNaZhi.getText().toString().equals("MPPT")) {
                        int mDaoNa = 255;
                        //得到导纳值输入框的内容
                        System_parameter[21] = (byte) (mDaoNa);
                    } else {
                        try {
                            mDaoNa2 = Integer.parseInt(mEtDaoNaZhi.getText().toString());

                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                        //得到导纳值输入框的内容
                        System_parameter[21] = (byte) (mDaoNa2);
                    }

                    //反解析欠压点
                    float ShortVoltage = Float.parseFloat(mEtShortVoltage.getText().toString());//		得到欠压点输入框的内容
                    System_parameter[12] = (byte) (ShortVoltage / (float) Kvdatacs);
                    //反解析输出过压点
                    float OutExcessiveVoltage = Float.parseFloat(mEtOutExcessiveVoltage.getText().toString());//		得到过压点输入框的内容
                    System_parameter[14] = (byte) (OutExcessiveVoltage / (float) Kvdatacs);
                    //反解析天亮检查点
                    float DayBreakVoltage = Float.parseFloat(mEtDayBreakVoltage.getText().toString());//		得到天亮点输入框的内容
                    System_parameter[15] = (byte) (DayBreakVoltage / (float) Kvdatacs);
                    //反解析天黑监测点

                    float DayDarkVoltage = Float.parseFloat(mEtDayDarkVoltage.getText().toString());                //		得到天黑点输入框的内容
                    System_parameter[16] = (byte) (DayDarkVoltage / (float) Kvdatacs);
                    //反解析欠压恢复点
                    float ShortVoltageRecover = Float.parseFloat(mEtShortVoltageRecover.getText().toString());                //		得到天黑点输入框的内容
                    System_parameter[13] = (byte) (ShortVoltageRecover / (float) Kvdatacs);

                    //使控制器运行

                    for (int i = 0; i < mRgLayout.getChildCount(); i++) {
                        RadioButton radioButton = (RadioButton) mRgLayout.getChildAt(i);
                        if (radioButton.isChecked()) {
                            if (i == 0) {
                                System_parameter[9] &= ~0x04;                //使控制器运行。。
                                Toast.makeText(getActivity(), "运行控制输出成功", Toast.LENGTH_SHORT).show();
                            } else {
                                System_parameter[9] |= 0x04;                //使控制器刹车。。
                                Toast.makeText(getActivity(), "运行控制刹车成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                        break;
                    }
                    // Load1半功率开始时间
                    if ((cs_out[0] & 0x0e) != 0) {
                        try {
                            mHalfTimeStartTime1 = Integer.parseInt(mEtHalfTimeStartTime.getText().toString());//	一半

                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }

                        System_parameter[24] = (byte) (mHalfTimeStartTime1);

                    }
                    //	Load1时空关时间
                    if ((cs_out[0] & 0x0e) != 0) {
                        try {
                            mTimeOffTime1 = Integer.parseInt(mEtTimeOffTime.getText().toString());
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }

                        System_parameter[25] = (byte) (mTimeOffTime1);
                        //  Log.i("mTimeOffTime1",(byte) ( mTimeOffTime1)+"");
                    }
                    //	Load2半功率开始时间
                    if ((cs_out[1] & 0x0e) != 0) {

                        try {
                            mHalfTimeStartTime2 = Integer.parseInt(mEtHalfTimeStartTime2.getText().toString());
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }

                        System_parameter[27] = (byte) (mHalfTimeStartTime2);

                    }
                    //	Load2时空关时间
                    if ((cs_out[1] & 0x0e) != 0) {

                        try {
                            mTimeOffTime2 = Integer.parseInt(mEtTimeOffTime2.getText().toString());
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }

                        System_parameter[28] = (byte) (this.mTimeOffTime2);

                    }
                    //输出控制
                    System_parameter[23] = cs_out[0];
                    System_parameter[26] = cs_out[1];
                    // 下发指令
                    ActualTimeFragment.Network_Send_flag = 4;                    //使通信管理切换到设定参数Save the parameters sucessfully
                    Toast.makeText(getActivity(), "保存参数成功", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ll_restore:
                // 恢复出厂参数
                ActualTimeFragment.Network_Send_flag = 3;
                Toast.makeText(getActivity(), "恢复出厂设置完成", Toast.LENGTH_SHORT).show();

                break;
        }

    }


    @Override
    public void onStop() {

        super.onStop();
        //取消RxBus订阅
        if (!mRxSbscription.isUnsubscribed()) {
            mRxSbscription.unsubscribe();
        }
    }


    // 根据屏幕宽度进行适配
    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    //设置适配的尺寸
    @Override
    public float getSizeInDp() {
        return 670;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // ButterKnife 解绑
        mUnbinder.unbind();
    }


}
